<template>
  <el-container class="home-container">
<!--    toubu-->
    <el-header>
      <div @click="btn" class="imgcol">
        <img src="../assets/user.png" width="50px" height="50px">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button></el-header>
<!--    主体-->
    <el-container>

      <el-aside :width="isCollapse ? '60px':'200px'">
        <div class="btns" >>>></div>
<!--        侧边栏-->
          <el-menu
            background-color="#373d38"
            text-color="#fff"
            active-text-color="#ffd04b" :unique-opened="true"  :collapse-transition="false"  :collapse="isCollapse" :router="true"
            :default-active="activePath">

            <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id" >
              <template slot="title">
                <i :class="iconsObj[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>

              <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id"
                            @click="saveActivePath('/' + subItem.path)">
                <template slot="title" >
                  <i class="el-icon-tickets"></i>
                  <span>{{subItem.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
      </el-aside>

      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  jjk: '>',
  data () {
    return {
      // 左侧菜单数据
      menuList: [],
      isCollapse: false,
      activePath: '',
      iconsObj: {
        147: 'iconfont icon-xin',
        125: 'iconfont icon-qunzu',
        103: 'iconfont icon-quanxian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-dingdan',
        145: 'iconfont icon-Activity'

      },
      welcome: {
        authName: '工作日记',
        id: 147,
        order: 1,
        path: '/love',
        activePath: '',
        children: [
          {
            authName: '心灵感悟',
            id: 124,
            order: 1,
            path: 'love',
            children: []
          }
        ]
      }
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },

  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      console.log(res)

      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      } else {
        this.$message.success(res.meta.msg)
        res.data.unshift(this.welcome)
        this.menuList = res.data
      }
    },
    saveActivePath (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
      // console.log(this.$route.path)
    },
    btn () {
      this.isCollapse = !this.isCollapse
    }
  }
}
</script>

<style lang="less" scoped>
.btns{
  background: #4a5064;
  font-size: 10px;
  text-align: center;
  line-height: 24px;
  color: #fff;
  letter-spacing: 0.8em;
}
.imgcol{cursor: pointer;}

.home-container{
  height: 100%;
  width: 100%;
  padding: 0;
}
.el-header{
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 10px;
  align-items: center;
  color: #eeeeee;
  font-size: 18px;
  >div{display: flex;align-items: center;
  span{
    margin-left: 20px;
  }
  }
}
.el-aside{
  background-color: #373d38;
  .el-menu{
    border-right: 0;
  }

}
.el-main{
  background-color: #eee;
}
</style>
